<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑往来账' : '新增往来账'"
    :width="660"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="记账类型">
          <a-select v-decorator="['types', { initialValue: model?.types , rules: [{ required: true, message: '请选择' }]}]" placeholder="请选择" @select="selectTypes">
            <a-select-option value="0">供应商回款</a-select-option>
            <a-select-option value="1">供应商退款</a-select-option>
            <a-select-option value="2">应收款</a-select-option>
            <a-select-option value="3">应付款</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="金额">
          <a-input-number v-decorator="['amount', { initialValue: model?.amount, rules: [{ required: true, message: '请输入金额' }] }]" style="width: 100%" />
        </a-form-item>
        <a-form-item label="记账账户" v-if="this.type == 1 || this.type == 0">
          <a-select v-decorator="['recordAccId', { initialValue: model?.recordAccId , rules: [{ required: true, message: '请选择' }]}]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in storeOption" :key="i.id">{{ i.accName }}</a-select-option>
          </a-select>
          供应商回款时是付款账户，供应商退款时是收款账户
        </a-form-item>
        <a-form-item label="记账日期">
          <a-date-picker v-decorator="['date', { initialValue: model?.date, rules: [{ required: true, message: '请选择' }] }]" style="width: 100%" name="date" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="备注">
          <a-input v-decorator="['memo', { initialValue: model?.memo }]" style="width: 100%" name="memo" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getStAccountListUsingGET } from '@/api/api/qiantaizhangwu.ts'
import pick from 'lodash.pick'
// 表单字段
const fields = ['types', 'amount', 'recordAccId', 'date', 'memo', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      type:'',
      form: this.$form.createForm(this),
      storeOption: [],
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
        typeList:'0,1'
      },
    }
  },
  mounted() {
    this.getOption()
  },
  methods: {
    selectTypes(e){
      this.type = e
    },
    getOption() {
      getStAccountListUsingGET(this.optionParam)
        .then((res) => {
          this.storeOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
}
</script>
